
function Udkp_Raid_OnLoad()

	local totalRaids = 0;
	totalRaids=getn(TEST_PARTY_STUFF);
	
	if(totalRaids>0) then
		Uasit_Debug("delete...",totalRaids,UdkpRaid_GetCurrentRaid)
		if(UdkpRaid_GetCurrentRaid~=0) then
			UdkpRaid_GetCurrentRaid=UdkpGetCurentRaid();
			
			if(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Status"]==-1) then
				getglobal("UdkpRaidStatus_Edit".."Text"):SetText("|cff0070dd"..UDKP_MESSAGE_STATUS_CREATE.."|r");
			elseif(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Status"]==0) then
				getglobal("UdkpRaidStatus_Edit".."Text"):SetText("|cff0070dd"..UDKP_MESSAGE_STATUS_HAVECREATE.."|r");
			elseif(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Status"]==1) then
				getglobal("UdkpRaidStatus_Edit".."Text"):SetText("|cff0070dd"..UDKP_MESSAGE_STATUS_ACTIVE.."|r");
			elseif(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Status"]==2) then
				getglobal("UdkpRaidStatus_Edit".."Text"):SetText("|cff0070dd"..UDKP_MESSAGE_STATUS_CLOSED.."|r");
			elseif(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Status"]==3) then
				getglobal("UdkpRaidStatus_Edit".."Text"):SetText("|cff0070dd"..UDKP_MESSAGE_STATUS_UPLOADED.."|r");
			end
			getglobal("UdkpRaidTime_Edit".."Text"):SetText(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["RaidTime"]);
			getglobal("Udkp_RaidMap_Edit"):SetText(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["RaidMap"]);
			getglobal("UdkpRaidName_Edit"):SetText(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Notes"]);
			getglobal("UdkpRaidLeader_Edit".."Text"):SetText(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["RaidLeader"]);
			getglobal("UdkpRaidStart_Edit".."Text"):SetText(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["StartTime"]);
			getglobal("UdkpRaidEnd_Edit".."Text"):SetText(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["EndTime"]);		
			getglobal("UdkpRaidHeaderString"):SetText(UDKP_MESSAGE_RAID.."|cff1eff00"..TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Notes"].." |r");
			if(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Team"]~="") then
				UIDropDownMenu_SetSelectedValue(getglobal("Udkp_TeamDropDown"), TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Team"]);
				Uasit_raidteamid=TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Team"];
			end
			
		end
		
	else
		HideUdkp_RaidFrameElement();
		getglobal("UdkpRaidHeaderString"):SetText(UDKP_MESSAGE_RAID);
	end	
	UdkpUpdateRaidList();
--	Uasit_UpdateMember();
--	Uasit_UpdateEvent();
--	Uasit_UpdateItem();
	UdkpChangeButtonStatus();
	
	
end

function Uasit_UpdateRaid()
	if(totalRaids>0) then
		if(UdkpRaid_GetCurrentRaid~=0) then
			
			getglobal("UdkpRaidTime_Edit".."Text"):SetText(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["RaidTime"]);
			getglobal("Udkp_RaidMap_Edit"):SetText(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["RaidMap"]);
			getglobal("UdkpRaidName_Edit"):SetText(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Notes"]);
			getglobal("UdkpRaidLeader_Edit".."Text"):SetText(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["RaidLeader"]);
			getglobal("UdkpRaidStart_Edit".."Text"):SetText(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["StartTime"]);
			getglobal("UdkpRaidEnd_Edit".."Text"):SetText(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["EndTime"]);
			--UIDropDownMenu_SetSelectedValue(getglobal("Udkp_TeamDropDown"), TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Team"]);
			--Uasit_raidteamid=TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Team"];
		end
	end
end

function HideUdkp_RaidFrameElement()

	getglobal("UdkpRaidFrameRaidTime"):Hide();
	getglobal("UdkpRaidFrameRaidMap"):Hide();
	getglobal("UdkpRaidFrameRaidNameTitle"):Hide();
	getglobal("UdkpRaidFrameRaidLeader"):Hide();
	getglobal("UdkpRaidFrameRaidTimeTitle"):Hide();
	getglobal("UdkpRaidFrameRaidEnd"):Hide();
	
	
	getglobal("UdkpRaidTime_Edit"):Hide();
	getglobal("Udkp_RaidMap_Edit"):Hide();
	getglobal("UdkpRaidName_Edit"):Hide();
	getglobal("UdkpRaidLeader_Edit"):Hide();
	getglobal("UdkpRaidStart_Edit"):Hide();
	getglobal("UdkpRaidEnd_Edit"):Hide();
	
	getglobal("UdkpSaveRaid_Button"):Hide();
	getglobal("UdkpRaidStart_Button"):Hide();
	getglobal("UdkpRaidEnd_Button"):Hide();
	getglobal("Udkp_MapDropDown"):Hide();
	
	getglobal("UdkpRaidFrameStatusTitle"):Hide();
	getglobal("UdkpRaidStatus_Edit"):Hide();
	
	getglobal("UdkpRaidFrameRaidTeam"):Hide();
	getglobal("Udkp_TeamDropDown"):Hide();
	
	
end


function ShowUdkp_RaidFrameElement()

	getglobal("UdkpRaidFrameRaidTime"):Show();
	getglobal("UdkpRaidFrameRaidMap"):Show();
	getglobal("UdkpRaidFrameRaidNameTitle"):Show();
	getglobal("UdkpRaidFrameRaidLeader"):Show();
	getglobal("UdkpRaidFrameRaidTimeTitle"):Show();
	getglobal("UdkpRaidFrameRaidEnd"):Show();
	
	
	getglobal("UdkpRaidTime_Edit"):Show();
	getglobal("Udkp_RaidMap_Edit"):Show();
	getglobal("UdkpRaidName_Edit"):Show();
	getglobal("UdkpRaidLeader_Edit"):Show();
	getglobal("UdkpRaidStart_Edit"):Show();
	getglobal("UdkpRaidEnd_Edit"):Show();
	getglobal("Udkp_MapDropDown"):Show();
	
	getglobal("UdkpSaveRaid_Button"):Show();
	getglobal("UdkpRaidStart_Button"):Show();
	getglobal("UdkpRaidEnd_Button"):Show();
	getglobal("UdkpRaidStart_Button"):Disable();
	getglobal("UdkpRaidEnd_Button"):Disable();
	
	getglobal("UdkpRaidFrameStatusTitle"):Show();
	getglobal("UdkpRaidStatus_Edit"):Show();
	
	getglobal("UdkpRaidFrameRaidTeam"):Show();
	getglobal("Udkp_TeamDropDown"):Show();
end

function UdkpSaveRaid()

	if(UdkpRaid_GetCurrentRaid~=0) then
			

			TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["RaidTime"]=getglobal("UdkpRaidTime_Edit".."Text"):GetText();
			TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["RaidMap"]=getglobal("Udkp_RaidMap_Edit"):GetText();
			TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Notes"]=getglobal("UdkpRaidName_Edit"):GetText();
			TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["RaidLeader"]=getglobal("UdkpRaidLeader_Edit".."Text"):GetText();
			TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["StartTime"]=getglobal("UdkpRaidStart_Edit".."Text"):GetText();
			TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["EndTime"]=getglobal("UdkpRaidEnd_Edit".."Text"):GetText();
			TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Team"]=Uasit_raidteamid;
	end
	if(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Status"]==-1) then
	TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Status"]=0;
	getglobal("UdkpRaidStatus_Edit".."Text"):SetText("|cff0070dd"..UDKP_MESSAGE_STATUS_HAVECREATE.."|r");
	end
	UdkpUpdateRaidList();
	UdkpChangeButtonStatus();
	
	getglobal("UdkpRaidHeaderString"):SetText(UDKP_MESSAGE_RAID.."(|cff1eff00"..TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Notes"].." |r)");
end


function UdkpStartRaid()
	TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["StartTime"]=Udkp_Date();
	TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["IntStartTime"]=time();
	TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Status"]=1;
	
	getglobal("UdkpRaidStart_Edit".."Text"):SetText(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["StartTime"]);		
	getglobal("UdkpRaidStart_Button"):Disable();
	getglobal("UdkpRaidEnd_Button"):Enable();
	local t = date("*t");
	
	for i = 1, GetNumRaidMembers(), 1 do
		
		
		local sPlayer = UnitName("raid" .. i);
		local _, race = UnitRace("raid" .. i);
		
		
		if(sPlayer ~= UdkpRaid_lang_UnknownEntity) then
			local name, rank, subgroup, level, class, fileName, zone, online = GetRaidRosterInfo(i);
			
			
			if(rank==2) then
				TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["RaidLeader"]=name;
			end
			if ( online==1 ) then
				table.insert(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"],1,
					{
						["player"] = sPlayer,
						["race"] = UdkpRaid_Race_Cn[race],
						["class"] = UdkpRaid_Class_Cn[fileName],
						["level"] = level,
						["join"] = t.hour..":"..t.min,
						["totaltime"] = 0,
						["dkp"] = 0,
						["ldkp"] = 0,
						["pdkp"] = getUdkpPlayerDKPLeft(sPlayer),
						["zone"] = zone,
						["rank"] = rank,
						["online"] = 1,
						["leavetime"] = time();
					}
				);
				--UdkpRaid_Offline[name] = true;
			else
				table.insert(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Members"],1,
					{
						["player"] = sPlayer,
						["race"] = UdkpRaid_Race_Cn[race],
						["class"] = UdkpRaid_Class_Cn[fileName],
						["level"] = level,
						["join"] = t.hour..":"..t.min,
						["totaltime"] = 0,
						["dkp"] = 0,
						["ldkp"] = 0,
						["pdkp"] = getUdkpPlayerDKPLeft(sPlayer),
						["zone"] = zone,
						["rank"] = rank,
						["online"] = 0,
						["leavetime"] = 0
					}
				);
				

				--UdkpRaid_GatheredData_Temp[name] = true;
			end
			--UdkpRaid_Online[name] = true;
		end
--		Uasit_UpdateMember();
	end
	Udkp_Raid_OnLoad();
	getglobal("UdkpRaidStatus_Edit".."Text"):SetText("|cff0070dd"..UDKP_MESSAGE_STATUS_ACTIVE.."|r");
	
end

function UdkpEndRaid()
	Udkp_Member_Compute();
	TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Status"]=2;
	TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["EndTime"]=Udkp_Date();
	getglobal("UdkpRaidEnd_Edit".."Text"):SetText(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["EndTime"]);
	getglobal("UdkpRaidStart_Button"):Disable();
	getglobal("UdkpRaidEnd_Button"):Disable();
	getglobal("Udkp_AddRaid_Button"):Enable();
	UdkpUpdateRaidList();
	getglobal("UdkpRaidStatus_Edit".."Text"):SetText("|cff0070dd"..UDKP_MESSAGE_STATUS_CLOSED.."|r");
	
end

function UdkpChangeMap()
	
  Udkp_RaidMap_Edit:SetText(this.value);
  local t=date("*t");
  UdkpRaidName_Edit:SetText(t.month.."-"..t.day..this.value.."("..getCurrentTeamName()..")");
  UIDropDownMenu_SetSelectedValue(Udkp_MapDropDown,this.value);
end

function Udkp_Map_Init()
  for k,instance in pairs(UDKP_MAP) do
    local info = { };
    info.text = instance;
    info.value = instance;
    info.func = UdkpChangeMap;
    UIDropDownMenu_AddButton(info);
  end
end


function Udkp_MapDropDown_OnShow()
  UIDropDownMenu_Initialize(Udkp_MapDropDown, Udkp_Map_Init);
  --UIDropDownMenu_SetText("", Udkp_MapDropDown);
 -- UIDropDownMenu_SetWidth(80, Udkp_MapDropDown);
 --UIDropDownMenu_SetSelectedID(Udkp_MapDropDown, 1);
 if(Udkp_RaidMap_Edit:GetText()=="") then
 		UIDropDownMenu_SetSelectedID(Udkp_MapDropDown, 1);
 	 	Udkp_RaidMap_Edit:SetText(UIDropDownMenu_GetSelectedValue(Udkp_MapDropDown));
 else
 		UIDropDownMenu_SetSelectedValue(Udkp_MapDropDown,Udkp_RaidMap_Edit:GetText());
 end
 
end

function UdkpChangeButtonStatus()

	totalRaids=getn(TEST_PARTY_STUFF);
	if(totalRaids>0) then
			getglobal("Udkp_RaidDelete_Button"):Enable();
			if(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Status"]==-1) then

				getglobal("UdkpSaveRaid_Button"):Enable();
				getglobal("Udkp_AddRaid_Button"):Disable();
				getglobal("UdkpRaidStart_Button"):Disable();
				getglobal("UdkpRaidEnd_Button"):Disable();
			elseif(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Status"]==0) then
				getglobal("UdkpSaveRaid_Button"):Enable();
				getglobal("Udkp_AddRaid_Button"):Disable();
				getglobal("UdkpRaidStart_Button"):Enable();
				getglobal("UdkpRaidEnd_Button"):Disable();
			elseif(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Status"]==1) then
				getglobal("UdkpSaveRaid_Button"):Enable();
				getglobal("Udkp_AddRaid_Button"):Disable();
				getglobal("UdkpRaidStart_Button"):Disable();
				getglobal("UdkpRaidEnd_Button"):Enable();
			elseif(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Status"]==2) then
				getglobal("UdkpSaveRaid_Button"):Enable();
				getglobal("Udkp_AddRaid_Button"):Enable();
				getglobal("UdkpRaidStart_Button"):Disable();
				getglobal("UdkpRaidEnd_Button"):Disable();
			elseif(TEST_PARTY_STUFF[UdkpRaid_GetCurrentRaid]["Status"]==3) then
				getglobal("UdkpSaveRaid_Button"):Enable();
				getglobal("Udkp_AddRaid_Button"):Disable();
				getglobal("UdkpRaidStart_Button"):Disable();
				getglobal("UdkpRaidEnd_Button"):Disable();
			end
		else
			getglobal("Udkp_RaidDelete_Button"):Disable();
			getglobal("Udkp_AddRaid_Button"):Enable();
		end
end